#include <iostream>

#include <nemesis.h>

#include <nemesis.io.BinaryReader.h>

using namespace std;

int main (int argc, const char** argv)
{
   CommandLine& commandLine (CommandLine::instanciate ());
   io::BinaryReader reader (32 * 1024);
   DataBlock contain (true);
   ZBlock zcompress, zuncompress;
   const DataBlock* block;
   ZBlock::Mode::_v mode;

   try {
      commandLine.add ("f", CommandLine::Argument::Mandatory, "Nombre del fichero a comprimir");
      commandLine.add ("m", CommandLine::Argument::Mandatory, "Mode (d,s,c)");

      commandLine.initialize (argv, argc);
      commandLine.verify ();

      // Lee el contenido del fichero indicado y lo guarda en el buffer 
      reader.open (commandLine.getValue ("f"));
      while ((block = reader.fetch ()) != NULL)
         contain += *block;

      const char* smode = commandLine.getValue ("m");

      switch (*smode) {
         case 'd': mode = ZBlock::Mode::Default; break;
         case 's': mode = ZBlock::Mode::BestSpeed; break;
         case 'c': mode = ZBlock::Mode::BestCompression; break;
      }

      cout << "Original: " << contain.getSize () << " bytes";
      cout << functions::asString (DataBlock (contain.getData (), min (128, contain.getSize ()), false), 24) << " (and more)" << endl << endl;

      zcompress.compress (contain, mode);
      cout << "Compress: " << zcompress.getSize () << " bytes";
      cout << functions::asString (DataBlock (zcompress.getData (), min (128, zcompress.getSize ()), false), 24) << " (and more)" << endl << endl;

      const DataBlock& uncompress = zuncompress.uncompress (zcompress);
      cout << "UnCompress: " << uncompress.getSize () << " bytes";
      cout << functions::asString (DataBlock (uncompress.getData (), min (128, uncompress.getSize ()), false), 24) << " (and more)" << endl << endl;
   }
   catch (Exception& ex) {
      cout << ex.asString () << endl;
   }
   
   return 0;
}
